package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.entity.AdminUser;
import com.hxzy.entity.HouseSize;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.HouseSizeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-09-15
 */
@Controller
@RequestMapping(value = "/admin")
public class HouseSizeController extends BaseController {

    @Autowired
    private HouseSizeService houseSizeService;

    @GetMapping(value = "/house/size/list")
    public String showPage(){
       return "house/size/list";
    }

    @ResponseBody
    @PostMapping(value = "/house/size/list")
    public List<HouseSize> ajaxData(){
        return this.houseSizeService.searchAll();
    }

    @GetMapping(value = "/house/size/add/{id}")
    public String addPage(@PathVariable(value = "id") Integer id, Model model){
        if(id==0){
            model.addAttribute("parentId",0);
            model.addAttribute("parentName","主目录");
        }else{
            HouseSize houseSize = this.houseSizeService.selectByPrimaryKey(id);
            if(houseSize==null){
                return "error/404";
            }
            model.addAttribute("parentId",id);
            model.addAttribute("parentName",houseSize.getName());
        }
        return "house/size/add";
    }


    @ResponseBody
    @PostMapping(value = "/house/size/add")
    public AjaxResult addData(HouseSize houseSize){
        AdminUser adminUser=(AdminUser) SecurityUtils.getSubject().getPrincipal();
        houseSize.setCreateTime(new Date());
        houseSize.setUpdateTime(new Date());
        houseSize.setCreateBy(adminUser.getLoginUserName());
        houseSize.setUpdateBy(adminUser.getLoginUserName());

        int count = this.houseSizeService.insert(houseSize);
        return super.toAjax(count);
    }

    @GetMapping(value = "/house/size/edit/{id}")
    public String editPage(@PathVariable(value = "id") Integer id, Model model){
        HouseSize houseSize = this.houseSizeService.selectByPrimaryKey(id);
        if(houseSize==null){
            return "error/404";
        }

        model.addAttribute("parentId",houseSize.getParentId());

        if(houseSize.getParentId()==0){
            model.addAttribute("parentName","主目录");
        }else{
            HouseSize parentSize = this.houseSizeService.selectByPrimaryKey(houseSize.getParentId());
            if(parentSize==null){
                return "error/404";
            }
            model.addAttribute("parentName",parentSize.getName());
        }
        model.addAttribute("houseSize",houseSize);
        return "house/size/edit";
    }


    @ResponseBody
    @PostMapping(value = "/house/size/edit")
    public AjaxResult editData(HouseSize houseSize){
        AdminUser adminUser=(AdminUser) SecurityUtils.getSubject().getPrincipal();
        houseSize.setUpdateTime(new Date());
        houseSize.setUpdateBy(adminUser.getLoginUserName());

        int count = this.houseSizeService.updateByPrimaryKeySelective(houseSize);
        return super.toAjax(count);
    }

}
